package com.carson.undergo.ui.activity.home;

import android.annotation.SuppressLint;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.carson.undergo.R;
import com.carson.undergo.databinding.ActivityCartDetailBinding;
import com.carson.undergo.ui.adapter.cart.CartGoodsAdapter;
import com.carson.undergo.ui.dialog.cart.ShopCartDialog;
import com.carson.undergo.utils.bean.CartGoodsBean;
import com.model.common.base.activity.BaseActivity;
import com.model.common.utils.always.StringUtils;
import com.model.common.utils.always.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by Carson on 2021/12/24.
 * Android仿饿了么购物车功能
 */
public class CartDetailUI extends BaseActivity<ActivityCartDetailBinding> {

    //商品列表
    private final List<CartGoodsBean> goodsList = new ArrayList<>();
    private CartGoodsAdapter cartGoodsAdapter;
    //购物车数量
    public int mCount = 0;
    //购物车信息
    public List<CartGoodsBean> cartList = new ArrayList<>();

    @Override
    protected ActivityCartDetailBinding getViewBinding() {
        return ActivityCartDetailBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initViewsAndEvents() {
        goodsList.add(new CartGoodsBean(1, "红豆奶茶", "饿了么专享", 15, 3));
        goodsList.add(new CartGoodsBean(12, "杨枝甘露", "饿了么专享", 8, 1));
        goodsList.add(new CartGoodsBean(666, "抹茶奶绿", "饿了么专享", 13.09, 7));
        goodsList.add(new CartGoodsBean(101, "冰红茶", "饿了么专享", 7.5, 10));
        //商品列表
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        cartGoodsAdapter = new CartGoodsAdapter(this, mContext, goodsList);
        binding.recyclerView.setAdapter(cartGoodsAdapter);
    }

    @Override
    protected void addListener() {
        super.addListener();
        binding.layoutCart.setOnClickListener(this);
        binding.tvSettlement.setOnClickListener(this);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    protected void onWidgetClick(View view) {
        super.onWidgetClick(view);
        switch (view.getId()) {
            case R.id.layout_cart://购物车列表
                if (mCount == 0) {
                    ToastUtils.showMessage(mContext, "请先添加商品");
                    return;
                }
                showCartDialog();
                break;
            case R.id.tv_settlement://结算
                if (mCount == 0) {
                    ToastUtils.showMessage(mContext, "请先添加商品");
                    return;
                }
                ToastUtils.showMessage(mContext, "结算咯~");
                break;
        }
    }

    /**
     * 购物车更改数量同步刷新列表数量
     */
    @SuppressLint("NotifyDataSetChanged")
    public void notifyMainGoods(int itemId, int count) {
        for (int i = 0; i < goodsList.size(); i++) {
            if (goodsList.get(i).getSkuId() == itemId) {
                goodsList.get(i).setCount(count);
            }
        }
        cartGoodsAdapter.notifyDataSetChanged();
    }

    /**
     * 展开购物车列表
     */
    private void showCartDialog() {
        /* 获取屏幕信息 */
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int height;
        if (cartList.size() > 4) {
            height = displayMetrics.heightPixels / 2;
        } else {
            height = ViewGroup.LayoutParams.WRAP_CONTENT;
        }
        ShopCartDialog dialog = new ShopCartDialog(this, mContext, R.style.cartDialog);
        Window window = dialog.getWindow();
        dialog.setCancelable(true);
        dialog.show();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = height;
        params.gravity = Gravity.BOTTOM;
        params.dimAmount = 0f;
        window.setAttributes(params);
    }

    /**
     * 得到购物车在窗体上的坐标
     */
    public int[] getCartLocation() {
        int[] cartLocation = new int[2];
        binding.layoutCart.getLocationInWindow(cartLocation);
        return cartLocation;
    }

    /**
     * 得到布局最外层
     */
    public RelativeLayout getContainer() {
        return binding.layoutFather;
    }

    /**
     * 更改购物车结算状态
     */
    public void setCartNumber() {
        if (mCount > 0) {
            binding.tvCartNumber.setText(String.valueOf(mCount));
            binding.tvCartNumber.setVisibility(View.VISIBLE);
            binding.tvSumPrice.setText(calculationTotalPrice());
            binding.tvSumPrice.setTextSize(16);
            binding.tvSumPrice.setTextColor(getResources().getColor(R.color.colorMain));
        } else {
            binding.tvCartNumber.setVisibility(View.GONE);
            binding.tvSumPrice.setText("请添加商品");
            binding.tvSumPrice.setTextSize(14);
            binding.tvSumPrice.setTextColor(getResources().getColor(R.color.colorHint));
        }
    }

    /**
     * 计算商品总价
     */
    private String calculationTotalPrice() {
        double price = 0;
        for (CartGoodsBean item : cartList) {
            if (item.getCount() > 0) {
                price += item.getCount() * item.getSellPrice();
            }
        }
        return StringUtils.formatDouble(price, true);
    }
}
